package com.hbx.residential_service.controller;

import com.github.pagehelper.PageInfo;
import com.hbx.residential_service.domain.*;
import com.hbx.residential_service.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //查询业主信息
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(Model model,HttpSession session, @RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<User> user = userService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(user);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("user-list");
        //session.getAttribute("adminname");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }
    //用户登录
    @RequestMapping("/login.do")
    public String userLogin(Model model, HttpSession session, String username, String userpwd) throws Exception {
        User user = userService.userLogin(username,userpwd);
        if(user!=null){
            session.setAttribute("user",user);
            session.setAttribute("username",user.getUsername());
            return "user-indirect";
        }else return "admin-failer";
    }
    //用户注册
    @RequestMapping("/register.do")
    public ModelAndView userRegister(User user){
        ModelAndView mv = new ModelAndView();
        userService.userRegister(user);
        mv.setViewName("user-login");
        return mv;
    }
    //修改密码
        //获取用户id
    @RequestMapping("/changepwd.do")
    public ModelAndView ChangePwd(HttpSession session){
        ModelAndView mv=new ModelAndView();
        User user = (User)session.getAttribute("user");
        String username = user.getUsername();
        user = userService.findByUserName(username);
        mv.addObject("user",user);
        mv.setViewName("user-changepwd");
        return mv;
    }
        //修改密码
    @RequestMapping("/updatepwd.do")
    public String UpdatePwd(User user){
        userService.UpdatePwd(user);
        return "user-login";
    }
    //跳转到主页面
    @RequestMapping("/main.do")
    public ModelAndView main(Model model, HttpSession session, @RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "4")int size,@RequestParam(name="page1",required = true,defaultValue = "1")int page1, @RequestParam(name = "size1",required = true,defaultValue = "4")int size1) throws Exception {
        ModelAndView mv = new ModelAndView();
        User user = (User)session.getAttribute("user");
        String username = user.getUsername();

        //我的投诉
        List<Complaint> cp = userService.userFindCp(username,page,size);
        PageInfo pageInfo = new PageInfo(cp);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        //我的报修
        List<Repair> rp = userService.userFindRp(username,page1,size1);
        PageInfo pageInfo1 = new PageInfo(rp);//重要 是一个分页bean
        mv.addObject("pageInfo1",pageInfo1);
        //公告
        List<Notice> notice = userService.FindNewNt();
        mv.addObject("notice",notice);
        //房屋
        List<House> house = userService.userFindHs(username);
        mv.addObject("house",house);
        //车位
        List<CarSite> carsite = userService.userFindCs(username);
        mv.addObject("carsite",carsite);
        //账单
        List<Pay> pay = userService.userFindPy(username);
        String usersex = userService.getUserSex(username);
        if(pay.size()!=0){
            mv.addObject("pay",pay);
            mv.addObject("username",username);
            if(usersex.equals("男"))mv.addObject("sex","先生：");
            else mv.addObject("sex","女士：");
            mv.addObject("title1","尊敬的");
            mv.addObject("title2","您需要缴纳如下费用");
            mv.addObject("title3","");
            mv.addObject("yuan","元");
        }else{
            mv.addObject("username",username);
            if(usersex.equals("男"))mv.addObject("sex","先生：");
            else mv.addObject("sex","女士：");
            mv.addObject("title1","尊敬的");
            mv.addObject("title2","");
            mv.addObject("title3","您目前无任何需缴纳费用，您的按时缴费，就是对我们最大的支持！");
        }

        mv.setViewName("user-main");
        model.addAttribute("username",username);
        return mv;
    }
    //投诉
    @RequestMapping("/complaint.do")
    public String addCp(Model model,HttpSession session,Complaint complaint)throws Exception{
        User user = (User)session.getAttribute("user");
        String username = user.getUsername();
        user = userService.findByUserName(username);
        model.addAttribute("user",user);
        userService.addCp(complaint);
        return "redirect:main.do";
    }
    //报修
    @RequestMapping("/repair.do")
    public String addRp(Model model,HttpSession session,Repair repair)throws Exception{
        User user = (User)session.getAttribute("user");
        String username = user.getUsername();
        user = userService.findByUserName(username);
        model.addAttribute("user",user);
        userService.addRp(repair);
        return "redirect:main.do";
    }
    //缴费
    @RequestMapping("/pay.do")
    public ModelAndView findNoPay(HttpSession session){
        User user = (User)session.getAttribute("user");
        String username = user.getUsername();
        ModelAndView mv = new ModelAndView();
        List<Pay> payList = userService.findNoPay(username);
        mv.addObject("paylist",payList);
        mv.setViewName("user-pay");
        return mv;
    }
    //wx缴费详情
    @RequestMapping("/paybywx.do")
    public ModelAndView findWxPayById(int payId){
        ModelAndView mv = new ModelAndView();
        Pay pay = userService.findPayById(payId);
        mv.addObject("pay", pay);
        mv.setViewName("user-pay-weixin");
        return mv;
    }
    //zfb缴费详情
    @RequestMapping("/paybyzfb.do")
    public ModelAndView findZfbPayById(int payId){
        ModelAndView mv = new ModelAndView();
        Pay pay = userService.findPayById(payId);
        mv.addObject("pay", pay);
        mv.setViewName("user-pay-zhifubao");
        return mv;
    }
    //完成支付
    @RequestMapping("/paydone.do")
    public String UpdatePay(Pay pay){
        userService.UpdatePay(pay);
        return "redirect:main.do";
    }
    //查找业主
    @RequestMapping("/findupdbyid.do")
    public ModelAndView findUpdUserById(int userid){
        ModelAndView mv = new ModelAndView();
        User user =userService.findUpdHouseById(userid);
        mv.addObject("user",user);
        mv.setViewName("user-update");
        return mv;
    }
    //修改业主信息
    @RequestMapping("/updbyid.do")
    public String UpdateById(User user){
        userService.UpdateById(user);
        return "redirect:findAll.do";
    }
    //模糊查询
    @RequestMapping("/fuzzysearchbyusername.do")
    public ModelAndView fsByUsername(Model model,HttpSession session,String content,@RequestParam(name="page",required = true,defaultValue = "1")int page, @RequestParam(name = "size",required = true,defaultValue = "5")int size)throws Exception{
        ModelAndView mv = new ModelAndView();
        content = new String(content.getBytes("iso-8859-1"),"utf-8");
        List<User> fsus = userService.fsByUsername(content,page,size);
        PageInfo pageInfo = new PageInfo(fsus);//重要 是一个分页bean
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("content",content);
        mv.setViewName("user-search");
        model.addAttribute("adminname",session.getAttribute("adminname"));
        return mv;
    }

}
